﻿Imports MICROFORMA.BEL
Imports MICROFORMA.DIL
Imports Microsoft.Practices.EnterpriseLibrary.Data
Imports System.Data.Common
Imports System.Data

Public Class DatosExternosDA
    Implements DatosExternosDI

    Public Function Add(ByVal datosExternosBE As BEL.DatosExternosBE) As String Implements DIL.DatosExternosDI.Add
        Dim db As Database = DatabaseFactory.CreateDatabase()

        Try
            Dim query As String = "Insert into DatosExternos (NombreConexion,CadenaConexion,FechaCreacion,UsuarioCreacion)" _
                                    & "Values ('" & datosExternosBE.NombreConexion & "','" & datosExternosBE.CadenaConexion _
                                    & "'," & "getDate()" & ",'" & datosExternosBE.UsuarioCreacion _
                                    & "')"


            Dim o_dbCommand As DbCommand = db.GetSqlStringCommand(query)


            db.ExecuteNonQuery(o_dbCommand)


            Return "OK"

        Catch ex As Exception

            Return ex.Message.ToString

        End Try
    End Function

    Public Function List() As DataTable Implements DIL.DatosExternosDI.List

        Dim db As Database = DatabaseFactory.CreateDatabase()

        Dim query As String = "select * from DatosExternos "

        Dim dbCommand As DbCommand = db.GetSqlStringCommand(query)

        Dim productsDataSet As DataSet = Nothing

        productsDataSet = db.ExecuteDataSet(dbCommand)


        Return productsDataSet.Tables(0)
    End Function

    Public Function Delete(ByVal idDatoExterno As Int16) As String Implements DIL.DatosExternosDI.Delete
        Dim db As Database = DatabaseFactory.CreateDatabase()

        If FindRegistryListaExterna(idDatoExterno) Then
            Return "Disculpe, No se puede Eliminar el Registro porque esta relacionado a un Repositorio Externo."
        End If

        Try
            Dim query As String = "Delete from DatosExternos where IdDatoExterno= '" & idDatoExterno & "'"

            Dim o_dbCommand As DbCommand = db.GetSqlStringCommand(query)

            db.ExecuteNonQuery(o_dbCommand)

            Return "OK"

        Catch ex As Exception

            Return ex.Message.ToString

        End Try
    End Function

    Function FindRegistryListaExterna(ByVal idDataExternal As Int16) As Boolean
        Dim db As Database = DatabaseFactory.CreateDatabase()

        Try
            Dim query As String = "select count(*) from listaExterna where IdDatoExterno= '" & idDataExternal & "'"

            Dim o_dbCommand As DbCommand = db.GetSqlStringCommand(query)

            If db.ExecuteScalar(o_dbCommand) <> 0 Then
                Return True
            Else
                Return False
            End If

        Catch ex As Exception

            Return False 'ex.Message.ToString

        End Try
    End Function

    Public Function Edit(ByVal datosExternosBE As BEL.DatosExternosBE) As String Implements DIL.DatosExternosDI.Edit
        Dim db As Database = DatabaseFactory.CreateDatabase()

        Try
            Dim query As String = "Update DatosExternos Set NombreConexion='" & datosExternosBE.NombreConexion _
                                 & "',CadenaConexion='" & datosExternosBE.CadenaConexion _
                                 & "',FechaModificacion=" & "getDate()" _
                                 & ",UsuarioModificacion='" & datosExternosBE.UsuarioModificacion _
                                 & "' where IdDatoExterno=" & datosExternosBE.IdDatoExterno


            Dim o_dbCommand As DbCommand = db.GetSqlStringCommand(query)


            db.ExecuteNonQuery(o_dbCommand)


            Return "OK"

        Catch ex As Exception

            Return ex.Message.ToString

        End Try
    End Function
End Class
